﻿using System;
using System.Linq;
using System.Globalization;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Data;

namespace Wp7nl.Converters
{
    namespace TimmyTools.WP7.Converters
    {
        /// <summary>
        /// Converter to create a nicer representation of an object.
        /// Add properties of the binded object between { }. The entered propertynames are not case sensitive.
        /// Example:
        /// add {firstname}{lastname} to the converter parameter 
        /// for binding to 'person' class Person.FirstName and Person.LastName
        /// </summary>
        public class ObjectToStringValueConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (parameter == null)
                {
                    return value.ToString();
                }

                string formatString = parameter.ToString().Replace("\\n", "\n");
                string resultstring = formatString;
                Regex r = new Regex(@"\{[a-z,A-Z]+(?:\.[a-z,A-Z]*)?\}");
                MatchCollection q = r.Matches(formatString);

                foreach (Match match in q)
                {
                    var extracted = this.ExtractValue(value, match.Value.Substring(1, match.Value.Length - 2));
                    if (!string.IsNullOrWhiteSpace(extracted))
                    {
                        resultstring = resultstring.Replace(match.Value, extracted);
                    }
                }
                return resultstring;
            }

            private string ExtractValue(object value, string match)
            {
                var type = value.GetType();
                PropertyInfo d = type.GetProperties().FirstOrDefault(x => x.Name.ToLower() == match.ToLower());
                if (d != null)
                {
                    return d.GetValue(value, null).ToString().Trim();
                }
                else
                {
                    return null;
                }
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return DependencyProperty.UnsetValue;
            }
        }
    }
}
